home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d20 / pulldir.arc / EDITSTR.C < prev    next >
C/C++ Source or Header  |  1991-04-21  |  5KB  |  218 lines

  1. /*
  2.  
  3.  * editstr.c
  4.  
  5.  * editing utility routines for general purpose use.
  6.  * copyright 1989, 1990 by Dana Bell
  7.  
  8.  * This file uses the stdio.h printf(). For functions that call the
  9.  * SwiftBBS fossil routine(s) (i.e. fosprintf()), use the file
  10.  * ansibbs.c instead.
  11.  
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <memory.h>
  18. #include <dos.h>
  19.  
  20. extern unsigned char field;
  21. extern ansi, trace;
  22. int count, n, keycode, txtpos, lastpos, maxpos,insert = 1;
  23. unsigned char edit[80];
  24. int FALSE = 0;
  25. int TRUE = 1;
  26.  
  27. unsigned navigate()
  28. {
  29.             switch(keycode) {
  30.                 case 75:
  31.                     /* -----Left arrow   */
  32.                     if (txtpos)
  33.                         txtpos--;                  /* Decrement the text pointer */
  34.                     break;
  35.                 case 77:
  36.                     /* -----Right arrow  */
  37.                     if (txtpos < lastpos)
  38.                         txtpos++;                  /* Increment the text pointer */
  39.                     break;
  40.                 case 72:
  41.                     /* -----Up     */
  42.                     if (field)
  43.                         field--;
  44.                     return(0);
  45.                 case 80:
  46.                     /* -----Down   */
  47.                     field++;
  48.                 case 73:
  49.                     /* -----Page Up   */
  50.                 case 81:
  51.                     /* -----Page Down */
  52.                     return(0);
  53.                 case 71:
  54.                     /* -----Home   */
  55.                     txtpos = 0;
  56.                     break;
  57.                 case 79:
  58.                     /* -----End    */
  59.                     txtpos = lastpos;
  60.                     break;
  61.                 case 82:
  62.                     /* -----Insert key   */
  63.                     insert = !insert;
  64.                     /* Adjust the cursor size  */
  65.                     break;
  66.                 case 83:
  67.                     /* -----Delete */
  68.                     if (txtpos < lastpos) {       /* Truncate the text */
  69.                         memmove(&edit[txtpos], &edit[txtpos+1], strlen(&edit[txtpos+1]));
  70.                         edit[maxpos-1] = 176;
  71.                         lastpos--;
  72.                     }
  73.                     break;
  74.                 default:
  75.                     return(1);
  76.                     /* return others  */
  77.             }
  78.     return(keycode);
  79. }
  80.  
  81. int editstr(char row, char col, const char *prompt, char *text, int maxlen)
  82. {
  83. /* ========================================================================
  84.  *            Displays an editable entry field for prompts
  85.  * ===================================================================== */
  86. /* unsigned char edit[80]; */
  87. char tab[4];
  88. char leftcol, rightcol, ch;
  89. char *genp = "none";
  90.  
  91.     strcpy(tab,"   ");            /* convert to 3 spaces  */
  92.     leftcol = col+strlen(prompt);
  93.  
  94.     locate(row, col);
  95.     printf("%s",prompt);
  96.     if((genp = strchr(prompt,'\n') != NULL)) {
  97.         row++;
  98.         leftcol = col+2;
  99.     }
  100.     rightcol = leftcol+maxlen;
  101.  
  102.     /* check field match */
  103.     if (strlen(text) > maxlen)
  104.         text[maxlen] = '\0';
  105.  
  106.     /* creates "edit" field, prints prompt, and locates to leftcol */
  107.     for(count = 0; count < rightcol-leftcol; count++)
  108.         edit[count] = 176;
  109.     edit[count] = '\0';
  110.     memcpy(edit,text,strlen(text));
  111.  
  112.     /* printf("%s",edit);   */
  113.     locate (row,leftcol);
  114.     maxpos = rightcol - leftcol;
  115.     txtpos = strlen(text);
  116.     lastpos = txtpos;
  117.     /* ----- Find the cursor's size in Scan Lines   */
  118.     /* -----Main loop for handling key presses   */
  119.  
  120.     do {
  121.         locate (row,leftcol);
  122.         printf("%s",edit);
  123.         locate (row, leftcol + txtpos);
  124.         while(!kbhit())
  125.             ;
  126.         if ((keycode = getch()) != 00)
  127.             switch (keycode) {
  128.                 case 3:
  129.                     return(0);
  130.                     /* temp control break escape  */
  131.                 case 0x8:
  132.                     /* -----Backspace */
  133.                     if (txtpos) {             /* Still within the field  */
  134.                         if (insert) {           /* truncate the string  */
  135.                             memmove(&edit[txtpos-1], &edit[txtpos], strlen(&edit[txtpos]));
  136.                             edit[maxpos-1] = 176;
  137.                         }
  138.                         else                    /* blank the letter  */
  139.                             edit[txtpos-1] = ' ';
  140.                     txtpos--;                  /* back up the text pointer   */
  141.                     lastpos--;
  142.                     }
  143.                     break;
  144.                 case 0x9:
  145.                     /* -----Tab */
  146.                     if (txtpos +2< maxpos) {
  147.                         if (insert) {                 /* expand the text string  */
  148.                             if (txtpos < maxpos - 3) {
  149.                                 memmove(&edit[txtpos+3],&edit[txtpos],strlen(&edit[txtpos+3]));
  150.                                 memcpy(&edit[txtpos],tab, 3);
  151.                                 printf("%s",&edit[txtpos]);
  152.                             }
  153.                         txtpos += 3;
  154.                         lastpos += 3;
  155.                         }
  156.                     }
  157.                     break;
  158.                 case 25:
  159.                     for(count = 0; count < rightcol-leftcol; count++)
  160.                         edit[count] = 176;
  161.                     edit[count] = '\0';
  162.                     lastpos = txtpos = 0;
  163.                     break;
  164.                 case 13:
  165.                     field++;
  166.                 case 26:
  167.                 fini:
  168.                     /* -----Enter, Escape, or Ctrl-Z */
  169.                     /* trim off fill, copy to edit, and return();   */
  170.                         for(count = maxpos-1;count > -1; count--) {
  171.                             if('░'==edit[count])
  172.                                 edit[count] = ' ';
  173.                             else
  174.                               break;
  175.                         }
  176.                         locate (row,leftcol);
  177.                         printf("%s",edit);
  178.                         for(count = maxpos-1;count > -1; count--) {
  179.                             if(' '==edit[count])
  180.                                 edit[count] = '\0';
  181.                             else
  182.                                 break;
  183.                         }
  184.                     strcpy(text,edit);
  185.                     return(keycode);
  186.  
  187.                 case 27:
  188.                     /* no change to original string */
  189.                     return(keycode);
  190.  
  191.                 default:
  192.                     /* -----Letter Keys  */
  193.                     if (txtpos < maxpos) {
  194.                         if (insert) {
  195.                             memmove(&edit[txtpos+1],&edit[txtpos],strlen(&edit[txtpos+1]));
  196.                             edit[txtpos] = keycode;
  197.                             printf("%s",&edit[txtpos]);
  198.                         }
  199.                         else {
  200.                             edit[txtpos] = keycode;
  201.                             putch(keycode);
  202.                         }
  203.                     txtpos++;
  204.                     lastpos++;
  205.                     }
  206.  
  207.             }  /* end of normal key switch   */
  208.         else { /* get extended key  */
  209.             keycode = getch();
  210.             if ((n = navigate()) == 0)
  211.                 goto fini;
  212.             else if(n == 1)
  213.                 return(keycode);
  214.             }
  215.     } while (1);
  216. }
  217.  
  218.